package com.prs.crm.service.flow.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.flow.WorkFlowProcessDao;
import com.prs.crm.domain.FlowEntity;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class WorkFlowProcessServiceImpl extends BaseServiceImpl<WorkFlowProcess,WorkFlowProcessDao>
	implements WorkFlowProcessService{
	

    
	public Collection<WorkFlowProcessNode> getNodeByStatus(WorkFlowProcess process,Integer status)
	{
		return this.getDao().getNodeByStatus(process, status);
	}
	
	
	public Collection<WorkFlowProcessNode> getNextProcessNodes(WorkFlowProcessNode node)
	{
		return this.getDao().getNextProcessNodes(node);
	}
	
	public void saveProcessNode(WorkFlowProcessNode node)
	{
		 this.getDao().saveProcessNode(node);
	}
	
	@SuppressWarnings("unchecked")
	public FlowEntity findProcessEntity(WorkFlowProcess process,Class cls)
	{
		return this.getDao().findProcessEntity(process, cls);
	}
	
	public void saveProcessEntity(FlowEntity entity)
	{
		this.getDao().saveProcessEntity(entity);
	}
	
	public Collection<WorkFlowProcessNode> getNodeByNodeType(WorkFlowProcess process,Integer nodeType)
	{
		return this.getDao().getNodeByNodeType(process, nodeType);
	}
	
	
	public int countProcessNodes(WorkFlowProcess process,Integer status,Integer result)
	{
		return this.getDao().countProcessNodes(process, status, result);
	}
	
	public int  updateProcessNode(final WorkFlowProcess process,final Integer status,final Integer result)
	{
		return this.getDao().updateProcessNode(process, status, result);
	}
	
	public PaginationSupport pageQueryFlowNode(Class<?> cls,PaginationSupport page,Person person,Integer[] status)
	{
		return this.getDao().pageQueryFlowNode(cls,page, person, status);
	}
	
	public PaginationSupport listFlowNodes(PaginationSupport page,WorkFlowProcess process,Integer[] status)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(WorkFlowProcessNode.class);
		criteria.add(Restrictions.eq("flowProcess", process)).add(Restrictions.in("status", status));
    	criteria.addOrder(Order.desc("id"));
    	return super.listByPage(criteria, page);
	}
	
	
	public 	Collection<WorkFlowProcessNode> pageHistoryFlowNode(FlowEntity entity)
	{
		return this.getNodeByStatus(entity.getProcess(), Constrains.WORKFLOWPROCESS_STATE_END);
	}


	@Override
	public WorkFlowProcess getWorkFlowProcess(Serializable id) {
		
		return this.getDao().getWorkFlowProcess(id);
	}


	@Override
	public List<WorkFlowProcessNode> getWorkFlowProcessNode(
			WorkFlowProcess flowProcess) {
		
		return this.getDao().getWorkFlowProcessNode(flowProcess);
	}


	@Override
	public void saveOrUpdate(WorkFlowProcess flowProcess) {
		
		this.getDao().saveOrUpdate(flowProcess);
		
	}


	@Override
	public void saveOrUpdateWorkFlowProcessNode(
			WorkFlowProcessNode workFlowProcessNode) {
		this.getDao().saveOrUpdateWorkFlowProcessNode(workFlowProcessNode);
		
	}

}
